//
//  ViewController.m
//  UI26_GCD
//
//  Created by dllo on 16/8/18.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIImageView *picImageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 1. GCD grand central dispatch(调度)
    // 2. GCD核心概念 队列 任务拆分 分配到队列中 系统自动为我们创建线程 并调度线程(自动的)
    // 3. CGD严格遵循FIFO(先进先出)原则
    // 4. 队列分类: 串行 并发
    //  1> 串行 DISPATCH_QUEUE_SERIAL / NULL
    //  2> 并发 DISPATCH_QUEUE_CONCURRENT
    // 5. 创建队列 参数1: 队列名 参数2: 队列类型
    
    // 创建串行队列
    dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
    // 创建并发队列
    dispatch_queue_t conCurrentQueue = dispatch_queue_create("conCurretQueue", DISPATCH_QUEUE_CONCURRENT);
    // 创建主队列 (串行)
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    // 全局队列 参数1: 调度队列优先级 参数2: 给未来使用 (占位的 填0)
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    
    // 串行: 一个任务执行完毕再执行下一个
    // 并发: 多个任务同时执行
    // 同步: 在当前线程执行任务 不能开辟线程
    // 异步: 在新的线程中执行任务 可以开辟新线程
    
    // 组只有异步 会开辟多个线程
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue1 = dispatch_queue_create("groupQueue", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_group_enter(group);
    dispatch_group_async(group, queue1, ^{
        NSLog(@"group1-----%@", [NSThread currentThread]);
        [self loadImage];
//        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://pic1.win4000.com/wallpaper/8/511ce1c9ee276.jpg"]];
//        [self performSelectorOnMainThread:@selector(onMain:) withObject:data waitUntilDone:YES];
    });
    dispatch_group_leave(group);

    
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        NSLog(@"group2-----%@", [NSThread currentThread]);
    });
    dispatch_group_leave(group);

    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        NSLog(@"group3-----%@", [NSThread currentThread]);
    });
    dispatch_group_leave(group);

    // 所有任务执行完毕会调用下面的方法
    dispatch_group_notify(group, mainQueue, ^{
        NSLog(@"所有操作执行完毕 刷新");
    });

    // 6. 同步  异步
    
    // 同步
#if 0
    // block中的内容会追加到队列中
    // 同步串行队列 (不会创建线程)(在主线程中执行)
//    [self syncSerialQueue:serialQueue];
//    dispatch_sync(serialQueue, ^{
//        for (int i = 0; i < 10; i++) {
//            NSLog(@"%d,%@", i, [NSThread currentThread]);
//        }
//    });
//    // 同步并发队列 (不会创建线程)(在主线程中执行)
//    [self syncConcurrent:conCurrentQueue];
//     dispatch_sync(conCurrentQueue, ^{
//         for (int i = 0; i < 10; i++) {
//             NSLog(@"%d,%@", i, [NSThread currentThread]);
//         }
//     });
//    // 同步主线程队列 (同线程同步同线程 不会执行 互相等待 导致程序不执行(9.0) 导致crush(10.0))
//    [self syncMain:mainQueue];
//    dispatch_sync(mainQueue, ^{
//        for (int i = 0; i < 10; i++) {
//            NSLog(@"%d,%@", i, [NSThread currentThread]);
//        }
//    });
//    // 同步全局队列 (不会创建线程)(并发的)
    [self syncGlobal:queue];
//    dispatch_sync(queue, ^{
//        for (int i = 0; i < 10; i++) {
//            NSLog(@"%d,%@", i, [NSThread currentThread]);
//        }
//    });
#endif
    
    // 异步
#if 0
    // 异步串行队列(创建一个新线程)
//    [self asyncSerialQueue:serialQueue];
//    dispatch_async(serialQueue, ^{
//        for (int i; i < 10; i++) {
//            NSLog(@"%d,%@", i, [NSThread currentThread]);
//        }
//    });
//    // 异步并发队列(创建新线程 根据任务决定需要创建多少线程)
//    [self asyncconCurrentQueue:conCurrentQueue];
//    dispatch_async(conCurrentQueue, ^{
//        for (int i; i < 10; i++) {
//            NSLog(@"%d,%@", i, [NSThread currentThread]);
//        }
//    });
//

    // 异步主队列 (不创建新线程)
    
//    [self asyncconMainQueue:mainQueue];
//    dispatch_async(mainQueue, ^{
//        for (int i; i < 10; i++) {
//            NSLog(@"%d,%@", i, [NSThread currentThread]);
//        }
//    });

//    [self asyncconGlobalQueue:queue];
//    // 异步全局队列 (创建新线程)
//    dispatch_async(queue, ^{
//        for (int i; i < 10; i++) {
//            NSLog(@"%d,%@", i, [NSThread currentThread]);
//        }
//    });
#endif
    
}

- (void)loadImage {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://pic1.win4000.com/wallpaper/8/511ce1c9ee276.jpg"]];
    dispatch_async(dispatch_get_main_queue(), ^{
        self.picImageView.image = [UIImage imageWithData:data];
        });
    });
}



- (void)onMain:(id)object {
    self.picImageView.image = [UIImage imageWithData:object];
}

// ------------------------------ 同步 --------------------------------
- (void)syncSerialQueue:(dispatch_queue_t)serialQueue {
    NSLog(@"同步串行队列");
    for (int i; i < 10; i++) {
        dispatch_sync(serialQueue, ^{
            NSLog(@"%d,%@", i, [NSThread currentThread]);
        });
    }
}

- (void)syncConcurrent:(dispatch_queue_t)concurrent {
    NSLog(@"同步并发队列");
    for (int i; i < 10; i++) {
        dispatch_sync(concurrent, ^{
            NSLog(@"%d,%@", i, [NSThread currentThread]);
        });
    }
}

- (void)syncMain:(dispatch_queue_t)mainqueue {
    NSLog(@"同步主队列");
    for (int i = 0; i < 10; i++) {
        dispatch_sync(mainqueue , ^{
            NSLog(@"%d, %@", i, [NSThread currentThread]);
        });
    }
}

- (void)syncGlobal:(dispatch_queue_t)global {
    NSLog(@"同步全局队列");
    for (int i = 0; i < 10; i++) {
        dispatch_sync(global , ^{
            NSLog(@"%d, %@", i, [NSThread currentThread]);
        });
    }
}

// -------------------------------异步-----------------------------------
- (void)asyncSerialQueue:(dispatch_queue_t)serialQueue {
    NSLog(@"异步串行队列");
     for (int i; i < 10; i++) {
    dispatch_async(serialQueue, ^{
       NSLog(@"%d,%@", i, [NSThread currentThread]);
    });
     }
}

- (void)asyncconCurrentQueue:(dispatch_queue_t)conCurrentQueue {
    NSLog(@"异步并发队列");
    for (int i; i < 10; i++) {
        dispatch_async(conCurrentQueue, ^{
            NSLog(@"%d,%@", i, [NSThread currentThread]);
        });
    }
}

- (void)asyncconMainQueue:(dispatch_queue_t)mainQueue {
    NSLog(@"异步主队列");
    for (int i; i < 10; i++) {
        dispatch_async(mainQueue, ^{
            NSLog(@"%d,%@", i, [NSThread currentThread]);
        });
    }
}

- (void)asyncconGlobalQueue:(dispatch_queue_t)queue {
    NSLog(@"异步全局队列");
    for (int i; i < 10; i++) {
        dispatch_async(queue, ^{
            NSLog(@"%d,%@", i, [NSThread currentThread]);
        });
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
